package prueba.principal.beans;

import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import prueba.principal.actions.UsersAction;
import prueba.principal.util.CheckSession;

public class UserBean {
	
	private int id;
	private String user;
	private String password;
	private String password2;
	private boolean error=false;	
	private HttpSession session;
	
	public UserBean() {
		FacesContext context= FacesContext.getCurrentInstance();
		session =(HttpSession)context.getExternalContext().getSession(true); 
		error=false;
	}
	
	public UserBean(int id,String user,String password){
		this.id=id;
		this.user=user;
		this.password=password;
	}

	public boolean isError() {
		return error;
	}


	public void setError(boolean error) {
		this.error = error;
	}



	public int getId() {
		return id;
	}



	public void setId(int id) {
		this.id = id;
	}



	public String getUser() {
		return user;
	}



	public void setUser(String user) {
		this.user = user;
	}



	public String getPassword() {
		return password;
	}
	
	



	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public void setPassword(String password) {
		this.password = password;
		
	}
	
	public String getLeft(){
		return CheckSession.sessionSecondsLeft();
	}
	
	
	//Validaciones
	
	public void validaPassword(FacesContext context, UIComponent component, Object value)  throws ValidatorException {
		
		String valor=(String) value;
		
		if(valor.length()<6){
			FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            message.setSummary("Password is too short.");
            message.setDetail("Password is too short.");
            context.addMessage("userForm:password", message);
            throw new ValidatorException(message);
		}
		/*
		if(password.toUpperCase().contains(user.toUpperCase())){
			FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            message.setSummary("Password is not valid. Must not contain the username.");
            message.setDetail("Password is not valid. Must not contain the username.");
            context.addMessage("userForm:password", message);
            throw new ValidatorException(message);
		}
		*/		
		
		if(!Pattern.matches("^[a-zA-Z0-9]+$", valor)){
			FacesMessage message = new FacesMessage();
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            message.setSummary("Password is not valid.");
            message.setDetail("Password is not valid.");
            context.addMessage("userForm:Email", message);
            throw new ValidatorException(message);
		}
		
		if(password2!=null){
			if(!password2.equals(valor)){
				FacesMessage message = new FacesMessage();
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				message.setSummary("Passwords don't match.");
				message.setDetail("Passwords don't match.");   
				context.addMessage("userForm:password", message);
				throw new ValidatorException(message);
			}
		}
		this.password=valor;
		
		
	}
	
	public void validaPassword2(FacesContext context, UIComponent component, Object value){
		
		
		String valor=(String) value;
		
				
		if(password!=null){
			if(!password.equals(valor)){
				FacesMessage message = new FacesMessage();
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				message.setSummary("Passwords don't match.");
				message.setDetail("Passwords don't match.");    
				context.addMessage("userForm:password", message);
				throw new ValidatorException(message);
			}
		}
		this.password2=valor;
	}


	//Navegacion

	public String login(){
		
		UsersAction login = new UsersAction();
		session.removeAttribute("username");
		
		if(login.compruebaUserPwd(user, password)){
			this.error=false;						
						
			session.setAttribute("username", user); 			
			
			return "OK";
		}else{
			this.error=true;
			return "KO";
		}
		
	}
	 
	public String logout(){
		
		session.removeAttribute("username");
		
		return "OK";
	}
	
	public String volver(){
				
		
		return "menu";
	}
	
	public String listUsers(){
		
		FacesContext context= FacesContext.getCurrentInstance();		
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
		
		UsersAction login = new UsersAction();
		
		request.setAttribute("lista",login.listaUsuarios());
		
		return "list";
		
	}
	
	public String formularioAddUser(){
		
		this.user="";
		
		
		return "addUser";
	}
	
	public String guardarUsuario(){
		
		UsersAction action = new UsersAction();
		
		action.addUsuario(user, password);		
		
		return "menu";
		
	}

	
	public String formulariodelUser(){
		
		return "delUser";
	}
}
